using System;
using System.Reflection;

namespace WinClient.View
{
	public class ValueGetterReflection : IValueGetter
	{
		private PropertyInfo _propertyInfo;

		public static bool IsNullable( Type type )
		{
			return type.IsGenericType && type.GetGenericTypeDefinition() == typeof ( Nullable<> );
		}

		public ValueGetterReflection( PropertyInfo propertyInfo )
		{
			_propertyInfo = propertyInfo;
		}

		#region IValueGetter Members

		public double GetValue( object item )
		{
			double result = 0;

			object value = _propertyInfo.GetValue( item, null );

			if( value != null )
			{
				result = Convert.ToDouble( value );								
			}

			return result;
		}

		#endregion
	}

	public class PropertyGetterReflection : IPropertyGetter
	{
		private PropertyInfo _propertyInfo;

		#region IPropertyGetter Members

		public object GetValue( object item, string propertyName )
		{
			_propertyInfo = item.GetType().GetProperty( propertyName );
			object value = _propertyInfo.GetValue( item, null );
			return value;
		}

		#endregion
	}
}